﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ExtVideoCateService
    {
        private readonly ExtVideoCateRepository _extVideoCate;
        private readonly ExtVideoRepository _extVideo;
        private readonly AdminUserGroupService _adminUserGroupService;
        private readonly ICacheManager _cacheManager;
        private readonly string cacheKey;
        public ExtVideoCateService(ExtVideoCateRepository  extVideoCateRepository, ExtVideoRepository  extVideoRepository, AdminUserGroupService adminUserGroupService, ICacheManager cacheManager)
        {
            _cacheManager = cacheManager;
            _extVideoCate = extVideoCateRepository;
            _extVideo = extVideoRepository;
            _adminUserGroupService = adminUserGroupService;
            cacheKey = _cacheManager.CacheName() + "CateList_Main";
        }


        public async Task<ExtVideoCate> GetInfo(int? id)
        {
            return await _extVideoCate.GetAsync(id);
        }

        /// <summary>
        /// 超级管理员获取全部列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<ExtVideoCate>> GetAllAsync()
        {
            List<ExtVideoCate> cate = _cacheManager.Get<List<ExtVideoCate>>(cacheKey);
            if (cate == null)
            {
                cate = await _extVideoCate.GetAllListAsync("Id desc");
                _cacheManager.Set(cacheKey, cate, TimeSpan.FromDays(10));
            }
            return cate;
        }

        /// <summary>
        /// 根据 CID 获取 一条栏目数据
        /// </summary>
        /// <returns></returns>
        public async Task<ExtVideoCate> GetOneAsync(int cid)
        {
            List<ExtVideoCate> cates = await GetAllAsync();
            return cates.FirstOrDefault(a => a.Id == cid);
        }
        /// <summary>
        /// 获取指定栏目列表
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="p">频道列表控制 默认关闭，不显示频道</param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public async Task<List<ExtVideoCate>> CateListByIdAsync(string cid, bool type = false, string orderBy = "Sort asc")
        {
            string cidCache = cacheKey + MD5Utility.MD5Encrypt16(cid);
            List<ExtVideoCate> cate = _cacheManager.Get<List<ExtVideoCate>>(cidCache);
            if (cate == null)
            {
                cate = await _extVideoCate.CateListByIdAsync(cid, type, orderBy);
                _cacheManager.Set(cidCache, cate, TimeSpan.FromDays(1));
            }
            return cate;
        }


        public async Task<object> InsertAsync(ExtVideoCate articleCate)
        {
            _cacheManager.RemoveCacheAsync(cacheKey);
            return await _extVideoCate.InsertAsync(articleCate);
        }

        public async Task<int> UpdateAsync(ExtVideoCate articleCate)
        {
            _cacheManager.RemoveCacheAsync(cacheKey);
            return await _extVideoCate.UpdateAsync(articleCate);
        }

        public async Task<int> DeleteByPriKeyAsync(int id)
        {
            ExtVideo isArticleHave = await _extVideo.GetByCidAsync(id);
            if (isArticleHave.Id > 0)
                return -1;
            _cacheManager.RemoveCacheAsync(cacheKey);
            return await _extVideoCate.DeleteByPriKeyAsync(id);
        }


        //获取当前用户角色的栏目权限信息
        public async Task<int[]> GetRoleChannlAsync(int gid)
        {
            AdminUserGroup userGroupInfo = await _adminUserGroupService.GetAsync(gid);
            string[] channlRole = JsonSerializer.Deserialize<string[]>(userGroupInfo.Channlrole.ToLower());
            return Array.ConvertAll(channlRole, int.Parse);
        }


        /// <summary>
        /// 查询栏目列表并转换
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<SelectListItem>> GetCategorySelectListItemsAsync()
        {
            List<ExtVideoCate> cates = await GetAllAsync();
            return cates.AsSelectListItems();//导出带 select标签样式的数据
        }
        public async Task<IEnumerable<ViewExtVideoCate>> GetCategoryListAsync()
        {
            List<ExtVideoCate> cates = await GetAllAsync();
            return cates.AsCateListItems();//导出带 所有栏目封装数据
        }

    }
}
